<template>
  <div class="login-page">
    <el-card class="login-page-card">
      <template #header>
        <div class="system">V50管理系统</div>
      </template>
      <el-form
        ref="ruleFormRef"
        :model="formInstance"
        :rules="validationRules"
        class="login-page-from"
        status-icon
      >
        <el-form-item
          class=".el-form-item__label"
          label="帐号："
          prop="userName"
        >
          <el-input v-model="formInstance.userName" placeholder="请输入账号" />
        </el-form-item>
        <el-form-item label="密码：" prop="password">
          <el-input
            type="password"
            v-model="formInstance.password"
            placeholder="请输入密码"
          />
        </el-form-item>
        <el-form-item class="form-btns">
          <el-button type="success" @click="submitForm(ruleFormRef)"
            >登录</el-button
          >
          <el-button type="danger" @click="resetForm(ruleFormRef)"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { mapActions } from "vuex";
export default {
  data() {
    return {
      formInstance: {
        userName: "",
        password: "",
      },
      validationRules: {
        userName: [
          {
            required: true,
            message: "请输入帐号",
            trigger: "blur",
          },
          {
            min: 3,
            max: 6,
            message: "请输入3到6位数字的帐号",
            trigger: "blur",
          },
        ],
        password: [
          {
            required: true,
            message: "请输入密码",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    ...mapActions({
      userInfoActions: "userInfoActions",
    }),
    submitForm() {
      // console.log("登录了");
      this.$refs.ruleFormRef.validate(async (valid, fields) => {
        if (valid) {
          // 在用户成功登录之前调用清理函数，清理掉state和本地化存储
          // 登录成功的逻辑
          const parmas = {
            adminname: this.formInstance.userName,
            password: this.formInstance.password,
          };
          // 登录成功
          const res = await this.userInfoActions(parmas);
          res && this.$router.push("/home/index");
        } else {
          // 登录失败
          console.log(fields);
          this.$message.error("登录失败");
        }
      });
    },
    // 重置用户输入的数据
    resetForm() {
      this.$refs.ruleFormRef.resetFields();
    },
  },
};
</script>

<style lang="scss" scoped>
.login-page {
  height: 100%;
  width: 100%;
  position: relative;
  background: url(../../assets/iTab-28mkg9.jpg) center center;
  background-size: cover;
  &-card {
    width: 400px;
    height: 300px;
    position: absolute;
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;
    margin: 9% auto;

    background: url(../../assets/m9jjp9.webp);
    ::v-deep .el-card__body {
      padding-top: 40px;
      .login-page-from {
        .form-btns {
          .el-form-item__content {
            justify-content: center;
          }
        }
        .el-form-item__label {
          color: red;
          font-size: 16px;
          font-weight: bolder;
        }
      }
    }
  }

  .system {
    text-align: center;
    font-size: 18px;
    font-weight: bolder;
    color: red;
  }
}
</style>
